package Passageiros;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Vo.PrecoTrechoVO;

public class PrecoTrechoDAO extends DataSourceDAO
{
	public PrecoTrechoDAO( ) 
	{
		
	}

    public PrecoTrechoVO consultarPrecoTrecho( int codigoPrecoTrecho ) throws DAOException  
    {
    	PrecoTrechoVO precoTrecho = null;
        String select = "SELECT * FROM PRECOTRECHO WHERE CODIGOPRECOTRECHO = ?";
        PreparedStatement pstmt	= null;
        ResultSet rs 			= null;
        
        try 
        {
        	connect( ) ;
            pstmt = connection.prepareStatement( select );
            pstmt.setInt( 1, codigoPrecoTrecho ) ;
            rs = pstmt.executeQuery( ) ;
            if ( rs.next() ) 
            {   
                double precoCrianca = rs.getDouble("PRECOCRIANCA");
            	double precoBebe = rs.getDouble("PRECOBEBE");
            	double precoAdulto = rs.getDouble("PRECOADULTO");
         
                precoTrecho = new PrecoTrechoVO( ); 
                precoTrecho.setCodigoPrecoTrecho( codigoPrecoTrecho );
                precoTrecho.setPrecoCrianca(precoCrianca);
                precoTrecho.setPrecoBebe(precoBebe); 
                precoTrecho.setPrecoAdulto(precoAdulto);
                
            }
        } 
        catch ( SQLException e ) 
        {
            e.printStackTrace();
            throw new DAOException("Impossivel consultar precoTrecho!", e );
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                if ( rs != null ) 
                {
                    rs.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
        return precoTrecho ;
    }
    
	public void inserirPrecoTrecho( PrecoTrechoVO precoTrecho ) throws DAOException  
    {
        String insert = "INSERT INTO PRECOTRECHO ( CODIGOPRECOTRECHO, PRECOCRIANCA, PRECOBEBE, PRECOADULTO) VALUES (?,?,?,?)";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement( insert );
            pstmt.setInt(1, precoTrecho.getCodigoPrecoTrecho());
            pstmt.setDouble(2,precoTrecho.getPrecoCrianca());
            pstmt.setDouble(3,precoTrecho.getPrecoBebe());
            pstmt.setDouble(4,precoTrecho.getPrecoAdulto());
               
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel inserir precoTrecho!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( );
            }
        }
    }
    
    public void excluirPrecoTrecho( PrecoTrechoVO precoTrecho )  throws DAOException  
    {
        String delete = "DELETE FROM PRECOTRECHO WHERE CODIGOPRECOTRECHO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement( delete );
            pstmt.setInt( 1, precoTrecho.getCodigoPrecoTrecho( ) ) ;
            pstmt.executeUpdate( ) ;
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel deletar precoTrecho!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace( ) ;
            }
        }
    }
    
    public void alterarPrecoTrecho( PrecoTrechoVO precoTrecho ) throws DAOException  
    {
        String insert = "UPDATE PRECOTRECHO SET PRECOCRIANCA = ?,PRECOBEBE = ? ,PRECOADULTO = ?  WHERE CODIGOPRECOTRECHO = ?";
        PreparedStatement pstmt = null;
        
        try 
        {
        	connect( ) ;
            connection.setAutoCommit( false );
            pstmt = connection.prepareStatement(insert);
            pstmt.setDouble(1,precoTrecho.getPrecoCrianca());
            pstmt.setDouble(2,precoTrecho.getPrecoBebe());
            pstmt.setDouble(3,precoTrecho.getPrecoAdulto());
            pstmt.setDouble(4,precoTrecho.getCodigoPrecoTrecho());
            pstmt.executeUpdate ();
            commit( ) ;
        } 
        catch ( SQLException e ) 
        {
            rollback( ) ;
            e.printStackTrace( ) ;
            throw new DAOException ( "Impossivel alterar precoTrecho!" , e ) ;
        } 
        finally 
        {
            try 
            {
                if ( pstmt != null ) 
                {
                    pstmt.close();
                }
                close( ) ;
            } 
            catch ( SQLException e ) 
            {
                e.printStackTrace();
            }
        }
    }
}
